<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Summary extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		
		$this->load->helper(array('form', 'url'));
		$this->load->config('overview', TRUE);
		$this->load->library('form_validation');
		$this->load->library('security');
		$this->load->library('tank_auth');
		$this->load->library('backyard_view');
		$this->load->library('session');
		$this->load->model('adsense/report');
		
		$this->lang->load('tank_auth');
	}

	function index()
	{
		//echo "is_logged_in:";var_dump($this->tank_auth->is_logged_in());exit;
		//echo 'hello world';	
        if (!$this->tank_auth->is_logged_in()) { // logged in
            redirect('/auth/login');
        
        } else {
            $data['online_website'] = $this->tank_auth->get_user_website();
			if($data['online_website'] == NULL){
				$this->load->view('welcome');
			}
            else {
                $data['online_website'] = explode('|', $data['online_website']); // explode website to an array
                /*
                 * get the first website overview once the page load
                 */
                $data['online_website_overview'] = $this->get_overview_by_website($data['online_website'][0]);
                $data['overview_table_terms'] = $this->config->item('overview_table_terms', 'overview');
				$data['bulletin_notice'] = $this->config->item('bulletin_notice', 'overview');
                $data['overview_table'] = $this->overview_table(&$data);
                //echo "<pre>";print_r($data);echo "</pre>";
                $this->load->view('summary/overview', $data);
            }            
        }
	}
	
	function get_overview(){
		sleep(1);
		if (!$this->tank_auth->is_logged_in()) { // logged in
            redirect('/auth/login');
        
        } else {
            $data['online_website'] = $this->tank_auth->get_user_website();
			if($data['online_website'] == NULL){
				$this->load->view('welcome');
			}
            else {
                $data['online_website'] = explode('|', $data['online_website']); // explode website to an array
                /*
                 * get the first website overview once the page load
                 */
                $data['online_website_overview'] = $this->get_overview_by_website($data['online_website'][0]);
                $data['overview_table_terms'] = $this->config->item('overview_table_terms', 'overview');
				$data['bulletin_notice'] = $this->config->item('bulletin_notice', 'overview');
                $data['overview_table'] = $this->overview_table(&$data);
                //echo "<pre>";print_r($data);echo "</pre>";
                $this->load->view('summary/overview_detail', $data);
            }            
        }
	}
	
	function get_account_settings(){
		sleep(1);
		$data = array();
		$account_settings_subterms = $this->config->item('account_settings_subterms', 'overview');
		$data['account_settings_subterms'] = '<div class="child01" id="subterms">';
		foreach($account_settings_subterms as $key=>$obj){
			
			$data['account_settings_subterms'] .= '<span class="'.$obj['class'].'"><a href="javascript:;" onclick="'.$obj['onclickjs'].'">● '.$obj['name'].'</a></span>';
		}
		$data['account_settings_subterms'] .= '</div>';
		$data_obj = $this->tank_auth->get_personal_info();
		$data['user_id'] = $data_obj->user_id;
		$data['user_name'] = $data_obj->user_name;
		$data['user_phone'] = $data_obj->user_phone;
		$data['user_address'] = $data_obj->user_address;
		$data['email'] = $this->session->userdata('email');
		
		$data['personal_info'] = $this->load->view('summary/personal_info',$data,true);
		
		
		echo json_encode($data);
	}
	
	function get_personal_info(){
		sleep(1);
		$data_obj = $this->tank_auth->get_personal_info();
		$data['user_id'] = $data_obj->user_id;
		$data['user_name'] = $data_obj->user_name;
		$data['user_phone'] = $data_obj->user_phone;
		$data['user_address'] = $data_obj->user_address;
		$data['email'] = $this->session->userdata('email');
		
		$this->load->view('summary/personal_info',$data);
	}
	
	function save_personal_info(){
		sleep(1);
		$userdata = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
		$update_status = array(); 
		if($this->tank_auth->save_personal_info(&$userdata)){
			$update_status['status'] = true;
			$update_status['msg'] = $this->config->item('update_personal_info_success', 'overview');;
			echo json_encode($update_status);
		}
		else{
			$update_status['status'] = false;
			$update_status['msg'] = $this->config->item('update_personal_info_fail', 'overview');
			echo json_encode($update_status);
		} 
		//$userdata = var_dump($userdata);
		//echo json_encode($userdata);
	}
	
	function get_change_password(){
		sleep(1);
		$this->load->view('summary/change_password');
	}
	
    function change_password() {
        sleep(1);
		$userdata = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
		$update_status = array(); 
		
		if($userdata['new_password'] !== $userdata['confirm_new_password']){
			$update_status['status'] = false;
			$update_status['msg'] = $this->config->item('change_password_fail_confirm_not_match', 'overview');
			echo json_encode($update_status);exit;
		}
		
        if ($this->tank_auth->change_password($userdata['old_password'], $userdata['new_password'])) { // success
            $update_status['status'] = true;
			$update_status['msg'] = $this->config->item('change_password_success', 'overview');
			echo json_encode($update_status);exit;
        
        } else { // fail
            $update_status['status'] = false;
			$update_status['msg'] = $this->config->item('change_password_fail', 'overview');
			echo json_encode($update_status);exit;
        }		
    }

	
	
	function get_overview_by_website($website){
		$overview = $this->report->get_overview_by_website($website);
		return $overview;
	}
	
	function overview_table($data){
		return $this->load->view('summary/overview_table',$data,true);
	}
	
	function get_overview_table(){
		sleep(1);
		$data['online_website_overview'] = $this->get_overview_by_website($this->input->post('website'));
		$data['overview_table_terms'] = $this->config->item('overview_table_terms', 'overview');
		echo $this->load->view('summary/overview_table',$data,true);
	}
	
	function get_report_chart(){
		
	}
	
	function get_report_data(){
		$json = array(
			array(
				array('2011-10-01', 1120), array('2011-10-02', 435), array('2011-10-03', 23), array('2011-10-28', 600), array('2011-10-31', 20), array('2011-10-4', 40), array('2011-10-5', 50), array('2011-11-01', 112), array('2011-11-02', 435), array('2011-11-03', 23), array('2011-11-28', 600), array('2011-11-31', 20), array('2011-11-4', 40), array('2011-11-5', 50)
			),
			array(
				array('2011-10-01', 1120), array('2011-10-02', 43500), array('2011-10-03', 203), array('2011-10-28', 1600), array('2011-10-31', 420), array('2011-10-4', 400), array('2011-10-5', 507), array('2011-11-01', 1120), array('2011-11-02', 43500), array('2011-11-03', 203), array('2011-11-28', 1600), array('2011-11-31', 420), array('2011-11-4', 400), array('2011-11-5', 507)
				)
		);
		echo json_encode($json);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */